import {Component, Inject, Optional} from '@angular/core';
import {MAT_DIALOG_DATA} from "@angular/material";
import {GnForm, GnMessageService} from "geminis-ng";
import {HttpClient, HttpHeaders, HttpParams} from "@angular/common/http";
import {GnFormConfig} from "../../../api/FormConfig";

@Component({
  selector: 'gn-workflow-user-work-do-form',
  templateUrl: './workForm.component.html'
})
export class WorkFormComponent extends GnForm {

  formConfig: GnFormConfig;

  constructor(@Inject(MAT_DIALOG_DATA) private param: any,
              private httpClient: HttpClient,
              private messageService: GnMessageService) {
    super();

    if (!param.assignee) {
      this.httpClient.post("/WORKFLOW-SERVICE/tasks/" + this.param.id + "/assignee", null)
        .subscribe(() => {
          },
          (error) => {
            const submitError = {
              message: "处理错误",
              error: error
            };
            this.messageService.error(submitError);
          });
    }

    this.get("/WORKFLOW-SERVICE/forms/task/" + this.param.processDefinitionId + "/" + this.param.taskDefinitionKey, "获取表单错误")
      .then((config: GnFormConfig) => {
        this.formConfig = config;
        this.get("/WORKFLOW-SERVICE/tasks/" + this.param.executionId + "/data", "获取数据错误")
          .then((value: any) => {
            this.formComponent.form.setValue(value);
          });
      });

    // this.httpClient.get("/WORKFLOW-SERVICE/forms/task/" + this.param.processDefinitionId + "/" + this.param.taskDefinitionKey)
    // .subscribe((data: GnFormConfig) => {
    //     this.formConfig = data;
    //     this.httpClient.get("/WORKFLOW-SERVICE/forms/task/" + this.param.processDefinitionId + "/" + this.param.taskDefinitionKey)
    //       .subscribe((data: GnFormConfig) => {
    //           this.formConfig = data;
    //         },
    //         (error) => {
    //           const submitError = {
    //             message: "获取表单错误",
    //             error: error
    //           };
    //           this.messageService.error(submitError);
    //         });
    //   },
    //   (error) => {
    //     const submitError = {
    //       message: "获取表单错误",
    //       error: error
    //     };
    //     this.messageService.error(submitError);
    //   });


  }

  complete(): void {
    this.httpClient.post("/WORKFLOW-SERVICE/tasks/" + this.param.id + "/complete", null)
      .subscribe(() => {
          this.dialog.confirmForm();
        },
        (error) => {
          const submitError = {
            message: "处理错误",
            error: error
          };
          this.messageService.error(submitError);
        });
  }

  get(url: string, errorMsg: string, options?: {
    headers?: HttpHeaders | {
      [header: string]: string | string[];
    };
    observe?: 'body';
    params?: HttpParams | {
      [param: string]: string | string[];
    };
    reportProgress?: boolean;
    responseType?: 'json';
    withCredentials?: boolean;
  }): Promise<void | object> {
    return this.httpClient.get(url, options)
      .toPromise()
      .catch(error => {
        const submitError = {
          message: errorMsg,
          error: error
        };
        this.messageService.error(submitError);
      });
  }

}
